﻿#include <iostream>
#include <csignal>

#include "AsioIOContextPool.h"
#include "CServer.h"
#include "ConfigMgr.h"

int main() {

  try {

    auto& cfg = ConfigMgr::GetInstance();
    auto port = cfg["ChatServer"]["port"];
    auto pool = AsioIOContextPool::GetInstance(); 
    boost::asio::io_context io_context;
    boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
    // 优雅的退出服务器
    signals.async_wait([&io_context,pool](auto, auto) {
      std::cout << "Server is stopping..." << std::endl;
      io_context.stop();
      pool->Stop();
      std::cout << "Server stopped." << std::endl;
    });

    CServer S(io_context, static_cast<uint16_t>(atoi(port.c_str())));
    io_context.run();
  } catch (const std::exception& e) {
    std::cerr << "Error: " << e.what() << std::endl;
    return 1;
  }

}
